//
//  ThemeViewController.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/18.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "ThemeViewController.h"
#import "CommunityCell.h"
#import "Community.h"
#import "CommentViewController.h"

@interface ThemeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** <#注释#> */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#注释#> */
@property (nonatomic, assign) NSInteger page;
@end
static NSString *const cellID = @"community_cell";
@implementation ThemeViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.title;
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.page = 1;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommunityCell" bundle:nil] forCellReuseIdentifier:cellID];
    NSLog(@"%@",self.themeid);
    [self setUpRefresh];
}

- (void)setUpRefresh {
    
    MJRefreshGifHeader *gifHeader = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        [self loadData];
        
        
    }];
    self.tableView.mj_header = gifHeader;
    [self.tableView.mj_header beginRefreshing];
    
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 34; i++) {
        NSString *str = [NSString stringWithFormat:@"46-120HQ24947－%d（被拖移）.tiff",i];
        UIImage *image = [UIImage imageNamed:str];
        [imageArray addObject:image];
    }
    gifHeader.labelLeftInset = -15;
    [gifHeader setImages:imageArray duration:0.5 forState:MJRefreshStateRefreshing];
    
    MJRefreshBackGifFooter *gifFooter = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self loadData];
        
    }];
    self.tableView.mj_footer = gifFooter;
    
    gifFooter.labelLeftInset = -15;
    [gifFooter setImages:imageArray duration:0.5 forState:MJRefreshStateRefreshing];
    
}

#pragma mark - 请求数据

- (void)loadData {
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"tokenid"] = @"tokenid_undefined";
    parame[@"userid"] = @"5784da4c4925c70c581758a9";
    parame[@"themeid"] = self.themeid;
    parame[@"time"] = @"1468831102514";
    parame[@"page"] = @1;
    parame[@"lastid"] = @0;
    
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/v53/square/themefitnote" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@", dic);
        for (NSDictionary *dict in dic[@"data"]) {
            Community *comm = [[Community alloc] init];
            [comm setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:comm];
        }
       
    } updateUI:^{
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommunityCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.community = self.dataArray[indexPath.row];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Community *comm = self.dataArray[indexPath.row];
    
    return comm.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentViewController *commentVc = [[CommentViewController alloc] init];
    commentVc.community = self.dataArray[indexPath.row];
    
    [self.navigationController pushViewController:commentVc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
